package org.wheel.plugins.starter.jdbc.pool.util;

import org.springframework.core.type.filter.TypeFilter;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;

import java.io.IOException;

public class InterfaceTypeFilter implements TypeFilter {

    private final Class<?> interfaceToScan;

    public InterfaceTypeFilter(Class<?> interfaceToScan) {
        this.interfaceToScan = interfaceToScan;
    }

    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
        String className = metadataReader.getClassMetadata().getClassName();
        try {
            Class<?> cls = Class.forName(className);
            return cls.isInterface() && interfaceToScan.isAssignableFrom(cls) && !cls.equals(interfaceToScan);
        } catch (ClassNotFoundException e) {
            return false;
        }
    }
}